package com.ruoyi.web.controller.ora;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ora.constant.BizConstants;
import com.ruoyi.ora.domain.Notebook;
import com.ruoyi.ora.factory.AiFactory;
import com.ruoyi.ora.service.INotebookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 记事Controller
 * 
 * @author liuwp
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/ora/notebook")
public class NotebookController extends BaseController
{
    @Autowired
    private INotebookService notebookService;

    /**
     * 查询记事列表
     */
    @PreAuthorize("@ss.hasPermi('ora:notebook:list')")
    @GetMapping("/list")
    public TableDataInfo list(Notebook notebook)
    {
        startPage();
        List<Notebook> list = notebookService.selectNotebookList(notebook);
        return getDataTable(list);
    }

    /**
     * 导出记事列表
     */
    @PreAuthorize("@ss.hasPermi('ora:notebook:export')")
    @Log(title = "记事", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Notebook notebook)
    {
        List<Notebook> list = notebookService.selectNotebookList(notebook);
        ExcelUtil<Notebook> util = new ExcelUtil<Notebook>(Notebook.class);
        util.exportExcel(response, list, "记事数据");
    }

    /**
     * 获取记事详细信息
     */
    @PreAuthorize("@ss.hasPermi('ora:notebook:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(notebookService.selectNotebookById(id));
    }

    /**
     * 新增记事
     */
    @PreAuthorize("@ss.hasPermi('ora:notebook:add')")
    @Log(title = "记事", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Notebook notebook)
    {
        return toAjax(notebookService.insertNotebook(notebook));
    }

    /**
     * 修改记事
     */
    @PreAuthorize("@ss.hasPermi('ora:notebook:edit')")
    @Log(title = "记事", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Notebook notebook)
    {
        return toAjax(notebookService.updateNotebook(notebook));
    }

    /**
     * 删除记事
     */
    @PreAuthorize("@ss.hasPermi('ora:notebook:remove')")
    @Log(title = "记事", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(notebookService.deleteNotebookByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('ora:notebook:list')")
    @GetMapping("/query")
    public TableDataInfo query(Notebook notebook)
    {
        notebook.setUserId(SecurityUtils.getUserId());
        List<Notebook> list = notebookService.selectNotebookList(notebook);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ora:voice_memoir:add')")
    @Log(title = "记事", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addNotebook(@RequestBody Notebook notebook)
    {
        notebook.setUserId(SecurityUtils.getUserId());
        return toAjax(notebookService.insertNotebook(notebook));
    }


    /**
     * 生成陪伴清单
     * @return
     */
    @GetMapping("/accompanying")
    public AjaxResult accompanying()
    {
        //  陪伴清单
        String content = AiFactory.build(BizConstants.AI_MODEL.getValue()).chatCompletions("请随机列出5条家庭互动事，直接给出即可不用举例，每条15字左右。例如：周末全家一起逛公园并野餐聊天，晚饭后和孩子共同读一本故事书，陪长辈整理旧相册聊过往趣事、周末下午和家人玩桌游增进感情等等");

        // 按 "数字. " 分割，并去掉空字符串
        String[] parts = content.split("\\s*\\d+\\.\\s*");

        // 由于 split 会在开头多一个空字符串，所以去掉第一个元素
        String[] activities = new String[parts.length - 1];
        System.arraycopy(parts, 1, activities, 0, activities.length);

        return AjaxResult.success(activities);
    }

    @Log(title = "记事", businessType = BusinessType.UPDATE)
    @PostMapping("/sort")
    public AjaxResult sort(@RequestBody List<Notebook> notebooks)
    {
        notebooks.forEach(notebook -> {
            notebookService.updateNotebook(notebook);
        });
        return AjaxResult.success();
    }

    /**
     * 生成代办
     * @return
     */
    @GetMapping("/agency")
    public AjaxResult agency()
    {
        //  陪伴清单
        String content = AiFactory.build(BizConstants.AI_MODEL.getValue()).chatCompletions("明天去爬山，晚上回来和朋友聚会。请根据以为内容生成出待办的清单，把时间和事项分开，时间使用年月日格式，不要使用明天后天等格式，使用json格式输出，直接json格式就行，不需要加markdown格式，字段名使用英文，例如：{\"time\":\"明天\",\"thing\":\"陪父母去爬山\"}，如果没有合适的事项直接返回空，不要随便举例");

//        // 按 "数字. " 分割，并去掉空字符串
//        String[] parts = content.split("\\s*\\d+\\.\\s*");
//
//        // 由于 split 会在开头多一个空字符串，所以去掉第一个元素
//        String[] activities = new String[parts.length - 1];
//        System.arraycopy(parts, 1, activities, 0, activities.length);

        return AjaxResult.success(content);
    }

}
